const db = uniCloud.database();

exports.main = async (event, context) => {
  try {
    const { status, page = 1, pageSize = 20 } = event;
    
    // 构建查询条件
    const query = db.collection('feedbacks');
    
    // 如果指定了状态，则添加状态过滤
    let queryCondition = {};
    if (status) {
      queryCondition.status = status;
    }
    
    // 执行分页查询，按创建时间倒序排列
    const skip = (page - 1) * pageSize;
    const records = await query
      .where(queryCondition)
      .orderBy('createTime', 'desc')
      .skip(skip)
      .limit(pageSize)
      .get();
    
    return {
      success: true,
      data: records.data,
      total: records.data.length,
      page,
      pageSize
    };
  } catch (error) {
    console.error('获取反馈列表失败:', error);
    return {
      success: false,
      message: '获取反馈列表失败',
      errorDetail: error.message || String(error)
    };
  }
};